#include "Amazon.h"

/**
Constructor
*/
Amazon::Amazon(void)
{
}

/**
Destructor
*/
Amazon::~Amazon(void)
{
}

/**
Recherche un identifiant pour un album
sur le web service d'Amazon
@param[in]	artist	nom de l'artiste
@param[in]	title	titre de l'album
*/
std::wstring Amazon::getId(const std::wstring& artist, const std::wstring title)
{
	wchar_t url[] = TEXT("http://webservices.amazon.com/onca/xml");
	std::map<std::wstring,std::wstring> map;
	map[TEXT("Service")] = TEXT("AWSECommerceService");
	map[TEXT("SubscriptionId")] = TEXT("04FVTXB5AE6NKW4JT3G2");
	map[TEXT("AssociateTag")] = TEXT("ASSOCIATES_ID");
	map[TEXT("Operation")] = TEXT("ItemSearch");
	map[TEXT("SearchIndex")] = TEXT("Music");
	map[TEXT("Artist")] = artist;
	map[TEXT("Title")] = title;
	WebService web;
	BSTR asin = web.getFirstID( web.request( url, map ) );
	return asin;
}

/**
Search an url on Amazon Webservice
@param artist	artiste name
@param	title	album name
*/
BSTR Amazon::search(const std::wstring& artist, const std::wstring title)
{
	wchar_t url[] = TEXT("http://webservices.amazon.com/onca/xml");
	std::map<std::wstring,std::wstring> map;
	map[TEXT("Service")] = TEXT("AWSECommerceService");
	map[TEXT("SubscriptionId")] = TEXT("04FVTXB5AE6NKW4JT3G2");
	map[TEXT("AssociateTag")] = TEXT("ASSOCIATES_ID");
	map[TEXT("Operation")] = TEXT("ItemLookup");
	map[TEXT("ItemId")] = getId(artist,title);
	map[TEXT("ResponseGroup")] = TEXT("Images");
	WebService web;
	return  web.getUrlLargeImage( web.request( url, map ));
}

/**
Download file from url
@param	url	URL file
@param	filename	local filename
*/
void Amazon::download(const std::wstring& url, const std::wstring filename)
{
	URLDownloadToFile( NULL, url.c_str(), filename.c_str(), 0, NULL );
}